package zabbix

import (
	"encoding/json"
	"errors"
)

func ServiceCreateData() (string,map[string]interface{}) {
	method := "service.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Server 1",
        "algorithm": 1,
        "showsla": 1,
        "goodsla": 99.99,
        "sortorder": 1
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceDeleteData() (string,map[string]interface{}) {
	method := "service.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"4",
		"5"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceDeletedependenciesData() (string,map[string]interface{}) {
	method := "service.deletedependencies"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"2"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceDeletetimesData() (string,map[string]interface{}) {
	method := "service.deletetimes"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"2"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceUpdateData() (string,map[string]interface{}) {
	method := "service.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "serviceid": "5",
        "parentid": "3"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceAdddependenciesData() (string,map[string]interface{}) {
	method := "service.adddependencies"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "serviceid": "3",
        "dependsOnServiceid": "2",
        "soft": 0
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceAddtimesData() (string,map[string]interface{}) {
	method := "service.addtimes"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "serviceid": "4",
        "type": 1,
        "ts_from": 165600,
        "ts_to": 201600
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceGetData() (string,map[string]interface{}) {
	method := "service.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "selectDependencies": "extend"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceGetslaData() (string,map[string]interface{}) {
	method := "service.getsla"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "serviceids": "2",
        "intervals": [
            {
                "from": 1352452201,
                "to": 1353057001
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func ServiceCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceDeletedependencies(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceDeletedependenciesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceDeletetimes(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceDeletetimesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceAdddependencies(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceAdddependenciesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceAddtimes(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceAddtimesData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func ServiceGetsla(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := ServiceGetslaData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}



